<?php
/**
 * 给课堂打分
 * User: yaosj
 * Date: 15-4-20
 * Time: 下午6:28
 */


class GradeClass implements iController{
    
    public function output($queryArgs) {
        global $cmsdb, $uddb;

        $userId      = $queryArgs['user_id'];
        $liveClassId = $queryArgs['live_class_id'];
        $teacherId   = $queryArgs['teacher_id'];
        $score       = $queryArgs['score'];
        $comment     = $queryArgs['comment'];

        if (empty($userId) || empty($liveClassId) || empty($teacherId) || empty($score)) {
            return genOutput(1000);
        }

        if (!empty($queryArgs['is_open']) && $queryArgs['is_open'] === 'true') {
            $liveClassId = $cmsdb->getVar('SELECT `class_id` FROM `relationship_live_class_and_course` WHERE `course_id`=:cid LIMIT 1',
                array(':cid' => $queryArgs['live_class_id']));
        }

        $sql = 'SELECT `grading_score` FROM `wl_grading_data` WHERE `user_id`=:user_id AND `live_class_id`=:class_id limit 1';

        $gradings = $uddb->getResults($sql,
            array(':user_id'  => $userId,
                  ':class_id' => $liveClassId
            ));

        //如果没有评价过，添加评价，不然直接返回；
        if (empty($gradings)) {

            $insertData = array (
                'user_id' => $userId,
                'live_class_id' => $liveClassId,
                'teacher_id' => $teacherId,
                'grading_score' => $score,
                'comment' => $comment
            );
            $uddb->insert('wl_grading_data',$insertData);
        }

        return genOutput(1);
    }
}
